package com.easylinkin.linkappapi.lobar.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easylinkin.linkappapi.common.exceptions.BusinessException;
import com.easylinkin.linkappapi.common.model.RequestModel;
import com.easylinkin.linkappapi.lobar.entity.UserTemperature;
import com.easylinkin.linkappapi.lobar.mapper.UserTemperatureMapper;
import com.easylinkin.linkappapi.lobar.service.UserTemperatureService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easylinkin.linkappapi.security.context.LinkappUserContextProducer;
import java.util.Date;
import java.util.Optional;
import javax.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 闸机表 服务实现类
 * </p>
 *
 * @author kanyuanfeng
 * @since 2022-04-13
 */
@Service
public class UserTemperatureServiceImpl extends ServiceImpl<UserTemperatureMapper, UserTemperature> implements UserTemperatureService {

  @Resource
  private LinkappUserContextProducer linkappUserContextProducer;

  /**
   * 增加
   * @param userTemperature
   */
  @Override
  public void insert(UserTemperature userTemperature) {
    /**
     * 验证重复
     */
    this.checkExist(userTemperature);
    //设置基本属性
    this.setBase(userTemperature);
    this.save(userTemperature);
  }

  /**
   * 根据id编辑
   * @param userTemperature
   */
  @Override
  public void updateOne(UserTemperature userTemperature) {
    /**
     * 验证重复
     */
    this.checkExist(userTemperature);
    //设置基本属性
    this.setBase(userTemperature);
    this.updateById(userTemperature);
  }

  @Override
  public IPage<UserTemperature> queryListByPage(RequestModel<UserTemperature> requestModel) {
    Page page = requestModel.getPage();
    UserTemperature userTemperature = requestModel.getCustomQueryParams();
    //项目id 过滤
    userTemperature.setTenantId(linkappUserContextProducer.getNotNullCurrent().getTenantId());
    IPage<UserTemperature> userTemperatureIPage = baseMapper.queryListByPage(page, userTemperature);
    return userTemperatureIPage;
  }

  /**
   * 验证重复
   */
  private void checkExist(UserTemperature userTemperature) {
    QueryWrapper<UserTemperature> queryWrapper = new QueryWrapper<>();
    //todo 设置判断重复条件
    //编辑的时候存在id
    Optional.ofNullable(userTemperature.getId()).ifPresent(id -> queryWrapper.ne("id",userTemperature.getId()));
    Integer integer = baseMapper.selectCount(queryWrapper);
    if (integer>0){
      throw new BusinessException("该闸机已存在");
    }
  }

  /**
   * 设置基本属性
   * @param userTemperature
   */
  private void setBase(UserTemperature userTemperature) {
    userTemperature.setModifyTime(new Date());
    userTemperature.setModifyId(linkappUserContextProducer.getCurrent().getId());
    //没有id就是新增,有就是编辑
    if (StringUtils.isBlank(userTemperature.getId())){
      userTemperature.setCreatorId(linkappUserContextProducer.getCurrent().getId());
      userTemperature.setCreateTime(new Date());
      userTemperature.setTenantId(linkappUserContextProducer.getNotNullCurrent().getTenantId());
    }
  }
}
